//
//  HMRedFlowerDetailsVC.m
//  优学荟管理系统
//
//  Created by 陈辉明 on 2017/12/19.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HMRedFlowerDetailsVC.h"
#import "HMRedFlowerDetailsCell.h"
#import "HMRedFlowerDetailsHeadView.h"
#import "HMRedFlowerDetailsModel.h"

@interface HMRedFlowerDetailsVC () <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong) HMRedFlowerDetailsHeadView *headView;
@property (nonatomic,strong) NSMutableArray <HMRedFlowerDetailsModel *>*dataSource;

//toatlCount :总计     stepName:标题  content:要素名字  要素数字:count
@end

@implementation HMRedFlowerDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpTableView];
    self.title = @"三好要素汇总表";
    [self setNetWork];
    self.headView =[[HMRedFlowerDetailsHeadView alloc]init];
}

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.tableHeaderView = self.headView;
}

- (void)setUpTableView {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"HMRedFlowerDetailsCell" bundle:nil] forCellReuseIdentifier:@"HMRedFlowerDetailsCellId"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 40;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    HMRedFlowerDetailsModel *model =  self.dataSource[section];
    return model.setCourseComments.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HMRedFlowerDetailsModel *model = self.dataSource[indexPath.section];
    HMRedFlowerSetDetailsModel *setModel = model.setCourseComments[indexPath.row];
    HMRedFlowerDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HMRedFlowerDetailsCellId" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model =setModel;
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HMRedFlowerDetailsModel *model = self.dataSource[section];
//    HMRedFlowerSetDetailsModel *setModel = model.setCourseComments[section.row];
    UIView *headView = [[UIView alloc]init];
    headView.frame = CGRectMake(0, 0, kScreenWidth - 30 , 44);
    headView.backgroundColor = [UIColor whiteColor];

    
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = [UIFont systemFontOfSize:17];
    titleLabel.textColor = RGBColor(51, 51, 51);
    titleLabel.text = [NSString stringWithFormat:@"%@",model.stepName];
    [headView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headView);
        make.left.offset(20);
    }];
    UIView *view1 = [[UIView alloc]init];
    view1.backgroundColor = RGBColor(245, 245, 245);
    [headView addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(1);
        make.left.offset(20);
        make.right.offset(-20);
        make.bottom.offset(0);
    }];
    
    return headView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    HMRedFlowerDetailsModel *model = self.dataSource[section];
    UIView *footView = [[UIView alloc]init];
    footView.backgroundColor = [UIColor whiteColor];
    footView.frame = CGRectMake(0, 0, kScreenWidth - 30 , 59);
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = [UIFont systemFontOfSize:17];
    titleLabel.textColor = RGBColor(51, 51, 51);
    titleLabel.text = @"总计";
    [footView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(12);
        make.left.offset(58.5);
    }];
    
    UILabel *countLabel = [UILabel new];
    countLabel.font = [UIFont systemFontOfSize:17];
    countLabel.textColor = RGBColor(51, 51, 51);
    countLabel.text = [NSString stringWithFormat:@"%@",model.toatlCount];
    [footView addSubview:countLabel];
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(12);
        make.right.offset(-20);
    }];
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = RGBColor(244, 244, 244);
    lineView.frame = CGRectMake(0, 44, kScreenWidth - 30, 15);
    [footView addSubview:lineView];
    

    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 59;
}

-(NSMutableArray<HMRedFlowerDetailsModel *> *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}



- (void)setNetWork{
    
    
   
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    //http://192.168.0.113:8080/StarShine/ws/trainLesson.do?action=getStepComment&enrollInfoId=1318
    NSString *url = [NSString stringWithFormat:@"trainLesson.do?action=getStepComment&enrollInfoId=%@&uid=%@",self.enrollInfoId,self.uid];
//    NSString *url = [NSString stringWithFormat:@"trainLesson.do?action=getStepComment&enrollInfoId=1318"];
    NSLog(@"%@ %@",LLBaseURLs(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
             [SVProgressHUD dismiss];
            if ([NSString isBlankString:Obj[@"t"][@"avatar"]]) {
                [self.headView.iconImage setImage:[UIImage imageNamed:@"bg_picture_newDefault"]];
            } else {
                [self.headView.iconImage sd_setImageWithURL:[NSURL URLWithString:Obj[@"t"][@"avatar"]] placeholderImage:[UIImage imageNamed:@"bg_picture_newDefault"]];
            }
            if ([NSString isBlankString:Obj[@"t"][@"petname"]]) {
                
            }else {
                self.headView.nameLabel.text = Obj[@"t"][@"petname"];
            }
            NSArray *t = [[Obj valueForKey:@"t"][@"stepCommentDetials"] isKindOfClass:[NSNull class]] ? @[]:[Obj valueForKey:@"t"][@"stepCommentDetials"];
            for (NSDictionary *dict  in t) {
                HMRedFlowerDetailsModel *model = [HMRedFlowerDetailsModel HMRedFlowerDetailsModelWithDict:dict];
                [self.dataSource addObject:model];
            }
            
            [self.tableView reloadData];
            
            
            
        } else {
            [SVProgressHUD showErrorWithStatus:Obj[@"msg"]];
        }
        
        
        
        
        
        
        
    } Fail:^(NSError *Error) {
        [SVProgressHUD showErrorWithStatus:NetErr];
    }];
    
    
    
    
    
    
    
    
    
}

-(void)viewWillDisappear:(BOOL)animated {
    [SVProgressHUD dismiss];
}



@end
